package com.darker.yygh.user.service.impl;

import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.darker.yygh.user.mapper.PatientApiMapper;
import com.darker.yygh.user.service.PatientApiService;
import com.darker.yyghcmn.client.DictFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientApiServiceImpl extends ServiceImpl<PatientApiMapper, Patient> implements PatientApiService {

    @Autowired
    private DictFeignClient dictFeignClient;


    //根据Id获取就诊人信息
    @Override
    public Patient getPatientById(Long id) {
        Patient patient = baseMapper.selectById(id);
        patient  = this.packPatient(patient); //添加件名称  联系人证件类型 省 市 区数据
        return patient;
    }

    //根据用户ID获取就诊人列表
    @Override
    public List<Patient> findAllByUserId(Long userId) {
        List<Patient> patients = baseMapper.
                selectList(new LambdaQueryWrapper<Patient>().eq(Patient::getUserId, userId));
        //通过远程调用得到编码对应具体内容 查询数据字典表内容
        patients.stream().forEach(item->{
            //其他参数封装
            this.packPatient(item);

        });

        return patients;
    }

    //封装证件名称  联系人证件类型 省 市 区数据
    private Patient packPatient(Patient patient) {
        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
        //联系人证件类型

//        String contactsCertificatesTypeString =
//                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        // TODO: 2022/6/4
        String contactsCertificatesTypeString =certificatesTypeString ;
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());


        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }

}

